/* 
 * File:   IPTEntry.h
 * Author: bharat
 *
 * Created on October 21, 2009, 10:00 PM
 */

#ifndef _IPTENTRY_H
#define	_IPTENTRY_H

#include "P2Defs.h"

#include "../machine/translate.h"
class TranslationEntry;

class IPTEntry : public TranslationEntry {
public:
    // page type related constatns
    static const int TYPE_UNDEFINED = 0 ;
    static const int TYPE_CODE_PAGE = 1;
    static const int TYPE_DATA_PAGE = 2;
 
    // page location relatted constatns
    static const int LOC_UNDEFINED = 0 ;
    static const int LOC_MEMORY = 1;
    static const int LOC_EXE_FILE = 2;
    static const int LOC_SWAP_FILE = 3;

    int pid; //owner process' ID
    int pageType; // page type- Code / user data
    int pageLocation; //where the page is right now? in momoey or swap file or in ececutable file
    int swapOffset; // if in swap file, than what is the swap offset.
    unsigned int age; // when did it came into memory ? used in FIFO page replacement policy

   // bool initComplete ; // this bit tell that IPTEntry has been refered in past or not. False initially, TRUE after first load.


    IPTEntry() {
        virtualPage = 0;
        physicalPage = 0;
        pid = -1;
        dirty = FALSE;
        readOnly = FALSE;
        valid = FALSE;
        use = FALSE;

        pageType = TYPE_UNDEFINED; 
        pageLocation = LOC_UNDEFINED; 
        age = 0; // this is a freash page
        swapOffset = -1; // since, not in swap file, swap offset is invalid value(-1)

     //   initComplete = false ;
    }

    IPTEntry(int ppn_, int vpn_, int pid_, bool dirty_, bool valid_, bool readOnly_, bool use_) {
        physicalPage = ppn_;
        virtualPage = vpn_;
        pid = pid_;
        dirty = dirty_;
        valid = valid_;
        readOnly = readOnly_;
        use = use_;

        pageType = TYPE_UNDEFINED; 
        pageLocation = LOC_UNDEFINED; 
        age = 0; // this is a freash page
        swapOffset = -1; // since, not in swap file, swap offset is invalid value(-1)

       // initComplete = false ;
    }

    void printIPTEntry() {
        printf("PPN:%d, VPN=%d, PID=%d, AGE=%u", physicalPage, virtualPage, pid, age);
        if (valid) {
            printf(", VALID:TRUE");
        } else {
            printf(", VALID:FALSE");
        }
        
        if (dirty) {
            printf(", DIRTY:TRUE");
        } else {
            printf(", DIRTY:FALSE");
        }

        if (pageType == TYPE_CODE_PAGE) {
            printf(", TYPE:CODE");
        } else if (pageType == TYPE_DATA_PAGE) {
            printf(", TYPE:DATA");
        }else  {
            printf(", TYPE:UNDEFINED");
        }

        if (pageLocation == LOC_MEMORY) {
            printf(", LOC:MEMORY");
        } else if (pageLocation == LOC_EXE_FILE) {
            printf(", LOC:EXEFILE");
        } else if (pageLocation == LOC_SWAP_FILE) {
            printf(", LOC:SWAPFILE , SWAPOFFSET: %d", swapOffset);
        } else {
            printf(", LOC:UNDEFINED");
        }

        /*
        if (initComplete){
            printf(", INITCOMPLETE:TRUE");
        }else{
            printf(", INITCOMPLETE:FALSE");
        }*/
        printf("\n");
    }
};

#endif	/* _IPTENTRY_H */

